package com.example.JPetStore.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.JPetStore.Common;
import com.example.JPetStore.domain.Cart;
import com.example.JPetStore.domain.Item;
import com.example.JPetStore.mappers.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemDao {

    @Autowired
    private ItemMapper itemMapper;

    public ArrayList<Item> queryItems(String productId){
        List<Item> itemList=itemMapper.selectList((new QueryWrapper<Item>()).eq("pro_id",productId));
        ArrayList<Item> itemArrayList=new ArrayList<>(itemList);
        return itemArrayList;
    }

    public Item queryItemById(String itemId) {
        Item item=itemMapper.selectById(itemId);
        return item;
    }

    public  ArrayList<Item> searchItems(String keyWord) {
        List<Item> itemList=itemMapper.selectList((new QueryWrapper<Item>()).like("item_id", keyWord).or().like("item_introduction",keyWord));
        ArrayList<Item> itemArrayList=new ArrayList<>(itemList);
        return itemArrayList;
    }

    public ArrayList<String> showItemsById(String keyWord) {
        keyWord="%"+keyWord+"%";
        List<Item> itemList=itemMapper.selectList((new QueryWrapper<Item>()).like("item_id", keyWord));
        ArrayList<String> itemArrayList=new ArrayList<>();
        for(Item i:itemList){
            itemArrayList.add(i.getItemId());
        }
        return itemArrayList;
    }

    public BigDecimal GainPrice(String itemId) {
        Item item=itemMapper.selectById(itemId);
        BigDecimal bd=new BigDecimal(item.getItemPrice());
        return bd;
    }

}
